'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview pvp大地图
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var modelUtil = require('../util/modelUtil');
var constantUtil = require('../util/constantUtil');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var pvpSchema = require('../schemas/pvpSchema');
var Pvp = mongoConnector().model('Pvp');
var buildModel = require('../models/buildModel');
var roleModel = require('../models/roleModel');
var pvpService = require('../services/pvpService');
var techModel = require('../../app/models/techModel');
var Tech = mongoConnector().model('Tech');
var buildType = require('../consts/buildType');
var dataApi = require('../libs/dataApi');
var rivalService = require('../services/rivalService');
var PlayerManager = require('../libs/playerManager');

/**
 * 根据role_id获取model
 * @return {[type]} [description]
 */
module.exports.getByRoleId = function (role_id, cb) {
    // 创建成功后
    function afterCreate(err, schema) {
        cb(null, schema);
    }

    // 查询成功后
    function afterFindOne(err, schema) {
        if (!!err) {
            return cb(err);
        }
        if (!!schema) {
            return cb(null, schema);
        } else {
            Pvp.create({
                role_id: role_id
            }, afterCreate);
        }
    }

    Pvp.findOne({
        role_id: role_id
    }, afterFindOne);
};

/**
 * 5点刷新
 */
module.exports.onFiveRefresh = function (self, callback) {
    self.resetFreeChangeRival();
    callback();
};

// /**
//  * 5点刷新
//  */
// module.exports.onFiveRefresh = function (self, callback) {
//     var role_id = self.role_id;
//     var player_manager = self.mgr;
//     if (!player_manager) {
//         player_manager = new PlayerManager(role_id);
//     }
// 
//     async.waterfall(
//         [
//             function (cb) {
//                 player_manager.getModel(
//                     ['role', 'build', 'tech', 'accrued'],
//                     cb
//                 );
//             },
//             function (cb) {
//                 var role_model = player_manager.role;
//                 var build_model = player_manager.build;
//                 var tech_model = player_manager.tech;
// 
//                 self.last_refresh_boss_time = moment();
//                 _refreshBoss(role_model, self);
//                 _refreshEvent(self, build_model, role_model);
// 
//                 cb();
//             }
//         ],
//         function (err) {
//             callback(err);
//         }
//     );
// };

/**
 * 刷新数据
 */
module.exports.onRefreshData = function (self, callback) {
    _initPlayerList(self, callback);
};

/**
 * 刷新boss
 * @param  {[type]} pvp_model [description]
 * @return {[type]}           [description]
 */
var _refreshBoss = function (role_model, pvp_model) {
    pvp_model.refreshBoss(role_model.getLv());
};

/**
 * 刷新事件
 * @param  {[type]} pvp_model [description]
 * @return {[type]}           [description]
 */
var _refreshEvent = function (pvp_model, build_model, role_model) {
    var nexus_build = build_model.getBuildByType(buildType.NEXUS);
    pvp_model.refreshEvent(role_model.getJob(), nexus_build.getLv(), role_model.getLv());
};

var _initPlayerList = function (pvp_model, cb) {
    var pvp_player_num = constantUtil.value('pvp_player_num');
    if (pvp_model.player_list.length >= pvp_player_num) {
        return cb();
    }
    var rival_list = [];
    for (var rival_num = pvp_model.player_list.length + 1; rival_num <= pvp_player_num; rival_num++) {
        rival_list.push(rival_num);
    }

    async.eachSeries(
        rival_list,
        function (rival_num, callback) {
            rivalService.getRandomRivalRoleId(pvp_model.role_id, pvp_model.getPlayerRoleIdList(), function (err, rival_role_id) {
                if (!!rival_role_id) {
                    pvp_model.addRoadPlayer({
                        road_id: rival_num,
                        role_id: rival_role_id
                    });
                }
                return callback();
            });
        },
        function (err) {
            return cb();
        }
    );
};
